﻿/*
 * Author: Trevor Vallender <tsv@tsv.me.uk>
 * Licence: Ms-PL
 */

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using PhotoOrg;

namespace PhotoOrganiserGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PhotoOrganiser po = new PhotoOrganiser();
        private string inputPath = "";
        private string outputPath = "";
        private bool recursive = false;
        private bool running = false; // Set to true while photos being copied.

        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Opens FolderBrowserDialog to choose input path.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BrowseInputButtonClick(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            DialogResult result = dialog.ShowDialog();

            InputText.Text = dialog.SelectedPath;
        }

        /// <summary>
        /// Opens FolderBrowserDialog to choose output path
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BrowseOutputButtonClick(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            DialogResult result = dialog.ShowDialog();

            OutputText.Text = dialog.SelectedPath;
        }

        /// <summary>
        /// Takes and checks input/output info, starts BackgroundWorker to sort photos.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OrganiseButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.inputPath = GetPath(this.InputText, "input");
                this.outputPath = GetPath(this.OutputText, "output");
            }
            catch (Exception ex)
            {
                return;
            }
            
            if ((bool)RecursiveCheckBox.IsChecked)
            {
                recursive = true;
            }
            else
            {
                recursive = false;
            }

            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += bw_Organise;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            bw.RunWorkerAsync();
            
            PBar.IsIndeterminate = true;
        }

        /// <summary>
        /// Checks a TextBox for a valid path and returns it.
        /// </summary>
        /// <param name="box">TextBox to parse.</param>
        /// <param name="name">Name of the path (used in error messages).</param>
        /// <returns>Path as a string.</returns>
        private string GetPath(System.Windows.Controls.TextBox box, string name)
        {
            MessageBoxButton button = MessageBoxButton.OK;
            MessageBoxImage icon = MessageBoxImage.Warning;

            if (box.Text != "")
            {
                if (System.IO.Directory.Exists(box.Text))
                {
                    return box.Text;
                }
                else
                {
                    System.Windows.MessageBox.Show("The "+name+" path does not exist.", "Path doesn't exist.", button, icon);
                }
            }
            else
            {
                System.Windows.MessageBox.Show("You have not entered a "+name+" path.", "No path entered.", button, icon);
            }

            throw new Exception(); // We should have returned a path by now!
        }

        /// <summary>
        /// Halt closing if we're in the middle of copying photos.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            if (this.running)
            {
                MessageBoxButton button = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Warning;
                string message = "Photos currently being sorted. Quitting will leave them unfinished. Are you sure you want to quit?";
                MessageBoxResult result = System.Windows.MessageBox.Show(message, "Really quit?", button, icon);
                
                if (result == MessageBoxResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        /// <summary>
        /// Calls PhotoOrganiser.Organise() from the BackgroundWorker.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bw_Organise(object sender, DoWorkEventArgs e)
        {
            this.running = true;
            this.po.Organise(this.inputPath, this.outputPath, this.recursive);
        }

        /// <summary>
        /// Fills progress bar, unsets this.running and displays complete message.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBoxButton button = MessageBoxButton.OK;
            MessageBoxImage icon = MessageBoxImage.Information;
            PBar.IsIndeterminate = false;
            PBar.Value = 100;
            this.running = false;
            System.Windows.MessageBox.Show("Your photos have been copied.", "Done", button, icon);
        }
    }
}
